﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServerClientCommon;

namespace SensorClient
{
    public class ClientMiniShell
    {
        #region Constants
        /// <summary>
        /// The default ip used
        /// </summary>
        private string DEFAULT_IP = "127.0.0.1";
        /// <summary>
        /// The default used port
        /// </summary>
        private int DEFAULT_PORT = 3000;
        /// <summary>
        /// The default module id
        /// </summary>
        private int DEFAULT_ID = 50;
        /// <summary>
        /// The help string which will be displayed
        /// </summary>
        private const string HELP_STRING =
        "Commanda:\n" +
        "START [serverIp] [port] [mode] [comPort] \n" +
        "serverIp - specifica ip-ul serverului\n" +
        "Exemplu: 127.0.0.1\n" +
        "port - portul pe care se va conecta clientul la server\n" +
        "Exemplu: 3000\n" +
        "mode - tipul de modul de teleasistenta utilizat\n" +
        "Exemplu: 0 - Emulator 1 - Crossbow 2 - FezPanda\n" +
        "comport - specifica portul de conectare la modulul de achizitie\n" +
        "Exemplu: COM1\n";
        /// <summary>
        /// The command argument index
        /// </summary>
        private const int COMMAND_ARG = 0;
        /// <summary>
        /// The server ip argument index
        /// </summary>
        private const int SERVER_IP_ARG = 1;
        /// <summary>
        /// The port argument index
        /// </summary>
        private const int PORT_ARG = 2;
        /// <summary>
        /// The mode argument index
        /// </summary>
        private const int MODE_ARG = 3;
        /// <summary>
        /// The serial port argument
        /// </summary>
        private const int COMPORT_ARG = 4;
        /// <summary>
        /// The maximum number of argumentss
        /// </summary>
        private const int NUMBER_OF_ARGS = 6;
        #endregion

        #region Constructors
        /// <summary>
        /// Creates a client mini shell
        /// </summary>
        public ClientMiniShell()
        {
            Console.Title = "Sensor Client";
            Console.ForegroundColor = ConsoleColor.Green;
        }
        #endregion

        /// <summary>
        /// Starts the shell
        /// </summary>
        public void Start()
        {
            Console.Write(HELP_STRING);
            bool settingsDone = false;
            string line = "";
            string[] pieces;
            NormalSensorsPreset nPreset = new NormalSensorsPreset();
            BooleanSensorsPreset bPreset = new BooleanSensorsPreset();
            SensorMaster master = null;
            while (settingsDone == false)
            {
                line = Console.ReadLine();
                try
                {
                    pieces = line.Split(' ');
                    settingsDone = true;
                    if (pieces[COMMAND_ARG].Equals("START"))
                    {
                        switch (pieces.Length)
                        {
                            case 1:
                                master = new SensorMaster(DEFAULT_ID, SensorConsumerType.EMULATOR, 
                                                          nPreset, bPreset,DEFAULT_IP,DEFAULT_PORT);
                                break;
                            case 2:
                                master = new SensorMaster(DEFAULT_ID, SensorConsumerType.EMULATOR,
                                                          nPreset, bPreset, pieces[SERVER_IP_ARG], DEFAULT_PORT);
                                break;
                            case 3:
                                master = new SensorMaster(DEFAULT_ID, SensorConsumerType.EMULATOR,
                                                          nPreset, bPreset, pieces[SERVER_IP_ARG], Convert.ToInt32(pieces[PORT_ARG]));
                                break;
                            case 4:
                                master = new SensorMaster(DEFAULT_ID, (SensorConsumerType)(Convert.ToInt32(pieces[MODE_ARG])),
                                                          nPreset, bPreset, pieces[SERVER_IP_ARG], Convert.ToInt32(pieces[PORT_ARG]));
                                break;
                            case 5:
                                master = new SensorMaster(DEFAULT_ID, (SensorConsumerType)(Convert.ToInt32(pieces[MODE_ARG])),
                                                          nPreset, bPreset, pieces[SERVER_IP_ARG], Convert.ToInt32(pieces[PORT_ARG]),
                                                          pieces[COMPORT_ARG]);
                                break;
                            default:
                                master = new SensorMaster(DEFAULT_ID, SensorConsumerType.EMULATOR, 
                                                          nPreset, bPreset,DEFAULT_IP,DEFAULT_PORT);
                                break;

                        }
                    }
                }
                catch (Exception ex)
                {
                    settingsDone = false;
                    Console.WriteLine("Error: " + ex.Message);
                }
            }
            master.Start();
        }
    }
}

